package com.wz.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.wz.bean.FestivalData;
import com.wz.bean.Msg;
import com.wz.fragment.FestivalCategoryFragment;
import com.wz.util.ToastUtil;

public class ChooseMesgActivity extends AppCompatActivity{
	
	
	private ListView mlvMesgs;
	private FloatingActionButton mfabToSend;
	private ArrayAdapter<Msg> madapter;
	private int fesId;
	private LayoutInflater minInflater;
	
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choose_mesg_activity);
	
		minInflater = LayoutInflater.from(this);
		
		Intent intent = this.getIntent();
		fesId = intent.getIntExtra(FestivalCategoryFragment.FESTIVAL_ID,-1);
		
		this.setTitle(FestivalData.getInstance().getFestivalById(fesId).getName());
		
		initViews();
	
		initEvent();
	}

	private void initEvent() {
		mfabToSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				SendMesgActivity.toActivity(ChooseMesgActivity.this,fesId, -1);
				
			}
		});
		
	}

	private void initViews() {
		mlvMesgs = (ListView) findViewById(R.id.wz_listview_msgs);
		mfabToSend = (FloatingActionButton) findViewById(R.id.wz_fab_toSend);
		List<Msg> msgs = FestivalData.getInstance().getMsgsByFestivalId(fesId);
		mlvMesgs.setAdapter(madapter = new ArrayAdapter<Msg>(this,-1,msgs){
			@Override
			public View getView( int position, View convertView, ViewGroup parent) {
				
				if(convertView == null){
					convertView = minInflater.inflate(R.layout.item_msg, parent,false);
				}
				
				TextView content = (TextView) convertView.findViewById(R.id.wz_tv_content);
				Button toSend = (Button) convertView.findViewById(R.id.wz_btn_toSend);
			
				content.setText(" "+this.getItem(position).getContent());
				final int mesgId = getItem(position).getId();
				toSend.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						SendMesgActivity.toActivity(ChooseMesgActivity.this,fesId, mesgId );
					}
				});
				
				return convertView;
			}
			
		});
	}

}











